package com.shopping.service.Impl;

import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import com.shopping.common.ServerResponse;
import com.shopping.dao.CategoryMapper;
import com.shopping.pojo.Category;
import com.shopping.service.ICategoryService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Set;

@Service("iCategoryService")
public class CategoryServiceImp implements ICategoryService{
    //获取类的日志
    private Logger logger= LoggerFactory.getLogger(CategoryServiceImp.class);
    @Autowired
    private CategoryMapper categoryMapper;
    public ServerResponse addCategory(String categoryName,Integer parentId){
    if (parentId==null || StringUtils.isBlank(categoryName)){
      return ServerResponse.createByErrorMessage("添加商品种类参数错误");
     }
     Category category=new Category();
    category.setName(categoryName);
    category.setParentId(parentId);
    category.setStatus(true);
    int rowCount =categoryMapper.insert(category);
    if (rowCount>0){
        return ServerResponse.createBySuccess("添加商品种类成功");
    }
    return ServerResponse.createByErrorMessage("添加商品失败");
    }

public ServerResponse updateCategoryName(Integer categoryId ,String categoryNam){
    if (categoryId==null || StringUtils.isBlank(categoryNam)){
        return ServerResponse.createByErrorMessage("更新品类参数错误");
    }
    Category category=new Category();
    category.setId(categoryId);
    category.setName(categoryNam);
    int rowCount=categoryMapper.updateByPrimaryKeySelective(category);
    if (rowCount>0){
        return ServerResponse.createBySuccess("更新品类成功！");
    }
    return ServerResponse.createByErrorMessage("更新品类名字失败");
  }
  public ServerResponse<List<Category>> getChildrenParallelCategory(Integer categoryId){
        List<Category> categoryList=categoryMapper.selectCategoryChildrenParenId(categoryId);
            //判断本身是否为空，同时也判断集合是否为空
        if (CollectionUtils.isEmpty(categoryList)){
            logger.info("未找到当前分类的子分类");
        }
        return ServerResponse.createBySuccess(categoryList);
  }
  //递归查询本节点的id，以及孩子节点的id
  public ServerResponse<List<Integer>> selectCategoryAndChildrenById(Integer categoryId){
        Set<Category> categorySet= Sets.newHashSet();
        findChildCategory(categorySet,categoryId);

        List<Integer>categoryList= Lists.newArrayList();
        if (categoryId!=null){
            for (Category categoryItem:categorySet){
                categoryList.add(categoryItem.getId());
            }
        }
        return ServerResponse.createBySuccess(categoryList);
  }
     //递归算法算出子节点
  private Set<Category>findChildCategory(Set<Category> categorySet,Integer categoryId){
      //小类别的ID
      Category category1=categoryMapper.selectByPrimaryKey(categoryId);
      if (category1!=null){
            categorySet.add(category1);
      }
      //查找递归子节点，递归算法一定要有个退出的条件,添加到HashSet里,寻找大类别的categoryId
      List<Category>categoryList=categoryMapper.selectCategoryChildrenParenId(categoryId);
      for (Category categoryItem:categoryList){
          findChildCategory(categorySet,categoryItem.getId());
      }

      return categorySet;
  }
}
